package net.srcz.jsjvm2;

import java.io.PrintStream;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;

public class JsClassVisitor implements ClassVisitor {

	PrintStream ps;
	
	public JsClassVisitor(PrintStream ps) {
		this.ps = ps;
	}
	
	@Override
	public void visit(int version, int access, String name, String signature,
			String superName, String[] interfaces) {
		ps.println("\""+name + "\"= function() {");
	}

	@Override
	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		return null;
	}

	@Override
	public void visitAttribute(Attribute attr) {
		ps.println("att " + attr.type);
	}

	@Override
	public void visitEnd() {
		ps.println("};");
	}

	@Override
	public FieldVisitor visitField(int access, String name, String desc,
			String signature, Object value) {
		ps.println("this.\""+name+"\" = "+ ConstantConverter.convert(value) + ";");
		return new JsFieldVisitor(ps);
	}

	@Override
	public void visitInnerClass(String name, String outerName,
			String innerName, int access) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public MethodVisitor visitMethod(int access, String name, String desc,
			String signature, String[] exceptions) {
		//boolean clinit = name.equals("<clinit>");
		//if(!clinit)
			ps.printf("this.\"%s%s\" = function() {\n",name,desc);
		return new JsMethodVisitor(ps,name,desc);
	}

	@Override
	public void visitOuterClass(String owner, String name, String desc) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visitSource(String source, String debug) {
	}

}
